package cn.hfatec.shms.system.resource.service;

import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.base.BaseDao;
import cn.hfatec.shms.advice.annotation.MysqlDataSource;
import cn.hfatec.shms.enums.KeyTypeEnum;
import cn.hfatec.shms.exception.KeyGenException;
import cn.hfatec.shms.system.authResource.dao.AuthResourceDao;
import cn.hfatec.shms.system.authResource.model.AuthResource;
import cn.hfatec.shms.system.resource.dao.ResourceDao;
import cn.hfatec.shms.system.resource.model.ResourceParams;
import cn.hfatec.shms.system.resource.model.ResourceView;
import cn.hfatec.shms.system.resource.model.Resources;
import cn.hfatec.shms.utils.KeyGenUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;



/**
 *
 * @author hfatec
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Service("ResourceService")
public class ResourceService {

    //注入Dao层
    @Resource(name = "ResourceDao")
    private ResourceDao resourceDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    @Resource(name = "AuthResourceDao")
    private AuthResourceDao authResourceDao;


    /**
     *
     * 查询数据
     *
     * @param resourceParams
     * @param page
     *
     */
    @Transactional
    @MysqlDataSource
    public Table queryResourceTable(ResourceParams resourceParams, Page page){

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", resourceParams);
        return baseDao.queryTableByPage("queryResource", params, page);
    }

    /**
     *
     * 插入数据
     * @param resource
     *
     */
    @Transactional
    @MysqlDataSource
    public void saveResource(Resources resource,String authId) throws KeyGenException {
        if(resource.getId() != null) {
            Resources result = getResource(resource.getId());
            if(result != null) {
                resourceDao.update(resource);
            } else {
                resource.setResourceId(KeyGenUtils.createKeyByType(KeyTypeEnum.RESOURCE_KEY));
                resourceDao.insert(resource);

                AuthResource authResource = new AuthResource();
                authResource.setAuthId(authId);
                authResource.setResourceId(resource.getResourceId());
                authResourceDao.insert(authResource);
            }
        } else {
            resource.setResourceId(KeyGenUtils.createKeyByType(KeyTypeEnum.RESOURCE_KEY));
            resourceDao.insert(resource);

            AuthResource authResource = new AuthResource();
            authResource.setAuthId(authId);
            authResource.setResourceId(resource.getResourceId());
            authResourceDao.insert(authResource);
        }
    }

    /**
     *
     * 删除数据
     * @param resourceId
     *
     */
    @Transactional
    @MysqlDataSource
    public void deleteResource(String resourceId) {
        Resources resources = resourceDao.getResourceByResourceId(resourceId);
        authResourceDao.deleteAuthResourceByResourceId(resourceId);
        resourceDao.delete(resources.getId());
    }

    /**
     *
     * 删除数据
     * @param resourceList
     *
     */
    @Transactional
    @MysqlDataSource
    public void deleteResourceBatch(List<String> resourceList) {
        for(String resourceId : resourceList) {
            deleteResource(resourceId);
        }
    }

    /**
     *
     * 根据ID获取数据
     * @param id
     *
     */
    @Transactional
    @MysqlDataSource
    public ResourceView getResource(Integer id) {
        return resourceDao.getResourceById(id);
    }
}